package cz.incad.psh.pshassign.server;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;

/**
 *
 * @author Alberto Hernandez
 */
public class Options {
    
    
    public static JSONObject load() throws Exception{
        String path = System.getProperty("user.home") + "/.psh/config.json";
        File f = new File(path);
        if(!f.exists()){
            f = FileUtils.toFile(Options.class.getResource("/cz/incad/psh/pshassign/config.json"));
        }
        String json = FileUtils.readFileToString(f, "UTF-8");
        return new JSONObject(json);
    }
    
    public static void save(String props) throws IOException{
        String path = System.getProperty("user.home") + "/.psh/config.json";
        FileUtils.writeStringToFile(new File(path), props, "UTF-8");
    }
    
   public static String getOption(String section, String option) throws Exception{
       JSONObject json = load();
      return json.getJSONObject(section).getString(option);
   }
    
   public static double getDouble(String section, String option) throws Exception{
       JSONObject json = load();
      return json.getJSONObject(section).getDouble(option);
   }
    
   public static int getInt(String section, String option) throws Exception{
       JSONObject json = load();
      return json.getJSONObject(section).getInt(option);
   }
}
